using micromsg;
using MicroMsg.Common.Utils;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Manager
{
	public static class SnsInfoMgr
	{
		private const string TAG = "SnsInfoMgr";
		private const ulong MinSendingObjectID = 18446744073709549615uL;
		public static TimeLineObject getTimeLine(SnsInfo snsInfo_0)
		{
			return snsInfo_0.getTimeLine();
		}
		public static void setTimeLine(SnsInfo snsInfo_0, TimeLineObject timeLineObject_0)
		{
			snsInfo_0.setTimeLine(timeLineObject_0);
		}
		public static int getCheckSum(SnsObject snsObject_0)
		{
			return Util.CheckSum(snsObject_0.ToByteArray());
		}
		private static SnsComment toSnsComment(SnsCommentInfo snsCommentInfo_0)
		{
			return new SnsComment
			{
				strUserName = snsCommentInfo_0.Username,
				strNickName = snsCommentInfo_0.Nickname,
				nSource = snsCommentInfo_0.Source,
				nType = snsCommentInfo_0.Type,
				strContent = snsCommentInfo_0.Content,
				nCreateTime = snsCommentInfo_0.CreateTime,
				nCommentId = snsCommentInfo_0.CommentId,
				nReplyCommentId = snsCommentInfo_0.ReplyCommentId,
				strReplyUsername = snsCommentInfo_0.ReplyUsername
			};
		}
		private static SnsCommentList toSnsCommentList(IList<SnsCommentInfo> ciList)
		{
			SnsCommentList snsCommentList = new SnsCommentList();
			using (IEnumerator<SnsCommentInfo> enumerator = ciList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsCommentInfo current = enumerator.get_Current();
					snsCommentList.list.Add(SnsInfoMgr.toSnsComment(current));
				}
			}
			return snsCommentList;
		}
		public static SnsInfo toSnsInfo(SnsObject snsObject_0)
		{
			SnsInfo snsInfo = new SnsInfo();
			snsInfo.nCheckSum = SnsInfoMgr.getCheckSum(snsObject_0);
			snsInfo.strObjectID = SnsInfo.toStrID(snsObject_0.Id);
			snsInfo.strUserName = snsObject_0.Username;
			snsInfo.strNickName = snsObject_0.Nickname;
			snsInfo.nCreateTime = snsObject_0.CreateTime;
			snsInfo.bytesObjectDesc = snsObject_0.ObjectDesc.Buffer.ToByteArray();
			snsInfo.nLikeFlag = (int)snsObject_0.LikeFlag;
			snsInfo.nLikeCount = (int)snsObject_0.LikeCount;
			snsInfo.likeList = SnsInfoMgr.toSnsCommentList(snsObject_0.LikeUserListList);
			snsInfo.nCommentCount = (int)snsObject_0.CommentCount;
			snsInfo.commentList = SnsInfoMgr.toSnsCommentList(snsObject_0.CommentUserListList);
			snsInfo.nWithUserCount = (int)snsObject_0.WithUserCount;
			snsInfo.withList = SnsInfoMgr.toSnsCommentList(snsObject_0.WithUserListList);
			snsInfo.nExtFlag = snsObject_0.ExtFlag;
			snsInfo.nStatus = 0;
			snsInfo.nIsRichText = ((snsObject_0.IsNotRichText == 0u) ? 1 : 0);
			SnsGroupList snsGroupList = new SnsGroupList();
			using (IEnumerator<SnsGroup> enumerator = snsObject_0.GroupListList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsGroup current = enumerator.get_Current();
					snsGroupList.list.Add(current.GroupId);
				}
			}
			snsInfo.groupList.nCount = (int)snsObject_0.GroupCount;
			snsInfo.groupList = snsGroupList;
			TimeLineObject timeLine = SnsInfoMgr.getTimeLine(snsInfo);
			if (timeLine != null && timeLine.content != null)
			{
				snsInfo.nObjectStyle = timeLine.content.nStyle;
			}
			return snsInfo;
		}
		public static List<SnsInfo> getList(List<ulong> idList)
		{
			List<string> list = new List<string>();
			using (List<ulong>.Enumerator enumerator = idList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ulong current = enumerator.get_Current();
					list.Add(SnsInfo.toStrID(current));
				}
			}
			return StorageMgr.snsInfo.getList(list);
		}
		public static SnsInfo smethod_0(ulong objectId)
		{
			return StorageMgr.snsInfo.getByObjectID(SnsInfo.toStrID(objectId));
		}
		public static bool updateList(IList<SnsObject> sojList)
		{
			if (sojList == null || sojList.get_Count() <= 0)
			{
				return false;
			}
			sojList = Enumerable.ToList<SnsObject>(Enumerable.Where<SnsObject>(sojList, (SnsObject snsObject_0) => snsObject_0.NoChange == 0u));
			if (sojList.get_Count() <= 0)
			{
				return false;
			}
			List<string> list = new List<string>();
			using (IEnumerator<SnsObject> enumerator = sojList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsObject current = enumerator.get_Current();
					list.Add(SnsInfo.toStrID(current.Id));
				}
			}
			Dictionary<string, int> checkSum = StorageMgr.snsInfo.getCheckSum(list);
			List<SnsInfo> list2 = new List<SnsInfo>();
			using (IEnumerator<SnsObject> enumerator2 = sojList.GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					SnsObject current2 = enumerator2.get_Current();
					if (!checkSum.ContainsKey(SnsInfo.toStrID(current2.Id)) || checkSum.get_Item(SnsInfo.toStrID(current2.Id)) != SnsInfoMgr.getCheckSum(current2))
					{
						list2.Add(SnsInfoMgr.toSnsInfo(current2));
					}
				}
			}
			return list2.get_Count() > 0 && StorageMgr.snsInfo.updateListByObjectID(list2);
		}
		public static void dumpSnsObjectList(IList<SnsObject> sojList)
		{
			using (IEnumerator<SnsObject> enumerator = sojList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsObject arg_10_0 = enumerator.get_Current();
				}
			}
		}
		public static bool update(SnsObject snsObject_0)
		{
			if (snsObject_0 != null)
			{
				if (snsObject_0.NoChange != 1u)
				{
					SnsInfo byObjectID = StorageMgr.snsInfo.getByObjectID(SnsInfo.toStrID(snsObject_0.Id));
					if (byObjectID == null)
					{
						return StorageMgr.snsInfo.method_3(SnsInfoMgr.toSnsInfo(snsObject_0));
					}
					return byObjectID.nCheckSum == SnsInfoMgr.getCheckSum(snsObject_0) || StorageMgr.snsInfo.updateByObjectID(SnsInfoMgr.toSnsInfo(snsObject_0));
				}
			}
			return false;
		}
		public static bool smethod_1(ulong objectID)
		{
			return StorageMgr.snsInfo.delByObjectID(SnsInfo.toStrID(objectID));
		}
		public static bool delList(List<ulong> objectIDList)
		{
			if (objectIDList == null || objectIDList.get_Count() <= 0)
			{
				return false;
			}
			if (objectIDList.get_Count() == 1)
			{
				return SnsInfoMgr.smethod_1(objectIDList.get_Item(0));
			}
			return StorageMgr.snsInfo.delList(objectIDList);
		}
		public static bool updateComment(SnsActionGroup snsActionGroup_0)
		{
			if (snsActionGroup_0 == null)
			{
				return false;
			}
			SnsInfo byObjectID = StorageMgr.snsInfo.getByObjectID(SnsInfo.toStrID(snsActionGroup_0.Id));
			if (byObjectID == null)
			{
				Log.smethod_5("SnsInfoMgr", "obj not exists in snsinfo, act.Id = " + snsActionGroup_0.Id);
				return true;
			}
			if (snsActionGroup_0.CurrentAction.Type == 1u)
			{
				using (List<SnsComment>.Enumerator enumerator = byObjectID.likeList.list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsComment current = enumerator.get_Current();
						if (current.nCommentId == snsActionGroup_0.CurrentAction.CommentId)
						{
							bool result = true;
							return result;
						}
					}
				}
				byObjectID.likeList.list.Add(SnsInfoMgr.toComment(snsActionGroup_0));
				byObjectID.likeListFlush();
			}
			else
			{
				if (snsActionGroup_0.CurrentAction.Type != 2u)
				{
					return false;
				}
				using (List<SnsComment>.Enumerator enumerator2 = byObjectID.commentList.list.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						SnsComment current2 = enumerator2.get_Current();
						if (current2.nCommentId == snsActionGroup_0.CurrentAction.CommentId)
						{
							bool result = true;
							return result;
						}
					}
				}
				byObjectID.commentList.list.Add(SnsInfoMgr.toComment(snsActionGroup_0));
				byObjectID.commentListFlush();
			}
			StorageMgr.snsInfo.update(byObjectID);
			return true;
		}
		public static SnsComment toComment(SnsActionGroup snsActionGroup_0)
		{
			if (snsActionGroup_0 == null)
			{
				return null;
			}
			return new SnsComment
			{
				strUserName = snsActionGroup_0.CurrentAction.FromUsername,
				strNickName = snsActionGroup_0.CurrentAction.FromNickname,
				nType = snsActionGroup_0.CurrentAction.Type,
				nSource = snsActionGroup_0.CurrentAction.Source,
				nCreateTime = snsActionGroup_0.CurrentAction.CreateTime,
				strContent = snsActionGroup_0.CurrentAction.Content,
				nCommentId = snsActionGroup_0.CurrentAction.CommentId,
				nReplyCommentId = snsActionGroup_0.CurrentAction.ReplyCommentId
			};
		}
		public static string allocObjectID()
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			ulong num;
			if (snsSendingObjectID.sending_ids.get_Count() <= 0)
			{
				num = 18446744073709549615uL;
			}
			else
			{
				num = Enumerable.Max<ulong>(snsSendingObjectID.sending_ids);
				num += 1uL;
			}
			snsSendingObjectID.sending_ids.Add(num);
			ConfigMgr.delayWrite<SnsSendingObjectID>(snsSendingObjectID);
			SnsPageMgr.notifySendingObjChanged();
			return SnsInfo.toStrID(num);
		}
		public static void releaseObjectID(string strObjID)
		{
			ulong num = SnsInfo.toID(strObjID);
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			if (snsSendingObjectID.sending_ids.Contains(num))
			{
				snsSendingObjectID.sending_ids.Remove(num);
				ConfigMgr.delayWrite<SnsSendingObjectID>(snsSendingObjectID);
			}
			SnsPageMgr.notifySendingObjChanged();
		}
		public static void delSendingObjectID(List<ulong> ids_del)
		{
			if (ids_del != null && ids_del.get_Count() > 0)
			{
				SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
				snsSendingObjectID.sending_ids = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(snsSendingObjectID.sending_ids, ids_del));
				ConfigMgr.delayWrite<SnsSendingObjectID>(snsSendingObjectID);
				SnsPageMgr.notifySendingObjChanged();
				return;
			}
		}
		public static void clearAllSendingObjectID()
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			snsSendingObjectID.sending_ids.Clear();
			snsSendingObjectID.sent_ids.Clear();
			ConfigMgr.delayWrite<SnsSendingObjectID>(snsSendingObjectID);
			SnsPageMgr.notifySendingObjChanged();
		}
		public static bool isSendingObjectID(ulong objID)
		{
			return objID >= 18446744073709549615uL;
		}
		public static List<ulong> getSendingObjectID()
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			if (snsSendingObjectID == null)
			{
				return null;
			}
			return snsSendingObjectID.sending_ids;
		}
		public static int getSendingObjectCount()
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			if (snsSendingObjectID == null)
			{
				return 0;
			}
			return snsSendingObjectID.sending_ids.get_Count();
		}
		public static List<ulong> getSentObjectID()
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			return snsSendingObjectID.sent_ids;
		}
		public static bool isSentObjectID(ulong objID)
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			return snsSendingObjectID != null && snsSendingObjectID.sent_ids.get_Count() > 0 && snsSendingObjectID.sent_ids.Contains(objID);
		}
		public static void addSentObjectID(ulong objID)
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			if (snsSendingObjectID.sending_ids.Contains(objID))
			{
				return;
			}
			snsSendingObjectID.sent_ids.Add(objID);
			ConfigMgr.delayWrite<SnsSendingObjectID>(snsSendingObjectID);
			SnsPageMgr.notifySendingObjChanged();
		}
		public static void delSentObjectID(List<ulong> list_0)
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			if (snsSendingObjectID.sent_ids != null && Enumerable.Count<ulong>(snsSendingObjectID.sent_ids) > 0)
			{
				snsSendingObjectID.sent_ids = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(snsSendingObjectID.sent_ids, list_0));
				ConfigMgr.delayWrite<SnsSendingObjectID>(snsSendingObjectID);
				SnsPageMgr.notifySendingObjChanged();
				return;
			}
		}
		public static List<SnsInfo> getNeedResumeList()
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			if (snsSendingObjectID.sending_ids.get_Count() <= 0)
			{
				return null;
			}
			List<SnsInfo> list = SnsInfoMgr.getList(snsSendingObjectID.sending_ids);
			if (list != null && list.get_Count() > 0)
			{
				return Enumerable.ToList<SnsInfo>(Enumerable.Where<SnsInfo>(list, (SnsInfo snsInfo_0) => snsInfo_0.nStatus == 1 || snsInfo_0.nStatus == 2));
			}
			return null;
		}
		public static List<SnsInfo> getErrorSendingObjectList()
		{
			SnsSendingObjectID snsSendingObjectID = ConfigMgr.smethod_0<SnsSendingObjectID>();
			if (snsSendingObjectID.sending_ids.get_Count() <= 0)
			{
				return null;
			}
			List<SnsInfo> list = SnsInfoMgr.getList(snsSendingObjectID.sending_ids);
			if (list != null && list.get_Count() > 0)
			{
				return Enumerable.ToList<SnsInfo>(Enumerable.Where<SnsInfo>(list, (SnsInfo snsInfo_0) => snsInfo_0.nStatus == 4));
			}
			return null;
		}
		public static void ClearAll()
		{
			StorageMgr.snsInfo.clearAll();
			SnsInfoMgr.clearAllSendingObjectID();
		}
		public static bool deleteSnsObject(ulong objectID, bool bChangeNow = true)
		{
			if (bChangeNow)
			{
				SnsInfoMgr.smethod_1(objectID);
				SnsMsgMgr.deleteMsg(objectID);
			}
			SnsAsyncMgr.delete(objectID, bChangeNow ? 0 : 1);
			return true;
		}
		public static bool setPrivate(ulong objectID, bool isPrivate, bool bChangeNow = true)
		{
			if (bChangeNow)
			{
				SnsInfo snsInfo = SnsInfoMgr.smethod_0(objectID);
				if (snsInfo == null)
				{
					return false;
				}
				snsInfo.setPrivacy(isPrivate);
				StorageMgr.snsInfo.modify(snsInfo);
			}
			SnsAsyncMgr.setPrivacy(objectID, isPrivate, bChangeNow ? 0 : 1);
			return true;
		}
		public static bool deleteComment(ulong objectID, int delCommentId, bool bChangeNow = true)
		{
			if (bChangeNow)
			{
				SnsInfo snsInfo = SnsInfoMgr.smethod_0(objectID);
				if (snsInfo == null)
				{
					return false;
				}
				snsInfo.deleteSnsComment(delCommentId);
				StorageMgr.snsInfo.modify(snsInfo);
			}
			SnsAsyncMgr.delComment(objectID, delCommentId, bChangeNow ? 0 : 1);
			return true;
		}
		public static bool setLikeFlag(ulong objectID, bool bLike, bool bChangeNow = true)
		{
			if (bLike)
			{
				return false;
			}
			if (bChangeNow)
			{
				SnsInfo snsInfo = SnsInfoMgr.smethod_0(objectID);
				if (snsInfo == null)
				{
					return false;
				}
				snsInfo.setLikeFlag(bLike, AccountMgr.strUsrName);
				StorageMgr.snsInfo.modify(snsInfo);
			}
			SnsAsyncMgr.setLikeFlag(objectID, bLike, bChangeNow ? 0 : 1);
			return true;
		}
		public static SnsTagInfo toSnsTagInfo(SnsTag snsTag_0)
		{
			SnsTagInfo snsTagInfo = new SnsTagInfo();
			snsTagInfo.nTagID = snsTag_0.TagId;
			snsTagInfo.strTagName = snsTag_0.TagName;
			snsTagInfo.nCount = (int)snsTag_0.Count;
			SnsTagMemberList snsTagMemberList = new SnsTagMemberList();
			using (IEnumerator<SKBuiltinString_t> enumerator = snsTag_0.ListList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SKBuiltinString_t current = enumerator.get_Current();
					snsTagMemberList.list.Add(current.String);
				}
			}
			snsTagInfo.memberList = snsTagMemberList;
			return snsTagInfo;
		}
	}
}
